package Homework.cms.HoutaiArticles;

import Homework.cms.articles.Articles;
import Homework.cms.articles.ArticlesDao;
import Homework.cms.util.Page;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class manageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");     //告诉它当前文档的编码
        response.setHeader("Content-Type","text/html;utf-8");    //告诉浏览器文档类型
        String action=request.getParameter("action");
        PrintWriter out=response.getWriter();   //获取一个打印流，向网页输出打印
        //管理员展示所有用户的作品
        if("page".equals(action)){
            int currPage=Integer.parseInt(request.getParameter("currPage"));
            Page<Articles> page= ArticlesDao.allusersarticles(10,currPage);
            request.setAttribute("page",page);
            request.getRequestDispatcher("adminarticlesmanage.jsp").forward(request,response);
        }
        //展示此用户的所有作品
        if("userpage".equals(action)){
            String writter=request.getParameter("writter");
            Articles articles=new Articles();
            articles.setWritter(writter);
            int currPage=Integer.parseInt(request.getParameter("currPage"));
            Page<Articles> page= ArticlesDao.usersarticles(10,currPage,articles);
            request.setAttribute("userpage",page);
            request.getRequestDispatcher("userarticlesmanage.jsp").forward(request,response);
        }
        //管理员删除用户作品，和用户删除自己作品网址一致，操作一致
        if("delete".equals(action)){
            String id=request.getParameter("id");
            Articles articles=new Articles();
            articles.setId(Integer.parseInt(id));
            ArticlesDao.remove(articles);
            String writter=request.getParameter("writter");
            //如果是用户删除作品，删除之后要返回用户的内容管理界面
            if(writter!=null){
                out.println("删除成功！");
                response.setHeader("Refresh","3;url=http://localhost:8080/webtest/Homework/manage/articles?action=userpage&currPage=1&writter="+writter);
            }
            //如果是管理员删除作品，删除之后要返回管理员的内容管理界面
            else {
                out.println("删除成功！");
                response.setHeader("Refresh","3;url=http://localhost:8080/webtest/Homework/manage/articles?action=page&currPage=1");
            }
        }

        if("update".equals(action)){
            int currPage=Integer.parseInt(request.getParameter("id"));
            Page<Articles> page=ArticlesDao.getPage(1,currPage);
            request.setAttribute("page",page);
            request.getRequestDispatcher("userarticlesupdate.jsp").forward(request,response);
        }

        if("add".equals(action)){
            int currPage=Integer.parseInt(request.getParameter("id"));
            Page<Articles> page=ArticlesDao.getPage(1,currPage);
            request.setAttribute("page",page);
            request.getRequestDispatcher("userarticlesadd.jsp").forward(request,response);
        }
    }
}
